package br.com.android.financas.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import br.com.android.financas.R;
import br.com.android.financas.model.Categoria;

public class CategoriaListaAdapter extends ArrayAdapter<Categoria> {

	private Activity activity;

	public CategoriaListaAdapter(Context context, int textViewResourceId, List<Categoria> items) {
		super(context, textViewResourceId, items);
		this.activity=(Activity) context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {

			LayoutInflater vi =activity.getLayoutInflater(); //getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.categoria_item, null);
		}
		Categoria categoria = getItem(position);

		TextView cor = (TextView) v.findViewById(R.categoriaLista.cor);
		cor.setBackgroundColor(categoria.getCor());
		fillText(v, R.categoriaLista.label, categoria.getNome());

		return v;
	}

	private void fillText(View v, int id, String text) {
		TextView textView = (TextView) v.findViewById(id);
		textView.setText(text == null ? "" : text);
	}
}